package com.ajmd.onlineexam.controller;

import com.ajmd.onlineexam.baseUtil.CommonResult;
import com.ajmd.onlineexam.config.MultiRequestBody;
import com.ajmd.onlineexam.entity.SearchParam;
import com.ajmd.onlineexam.entity.TeamPO;
import com.ajmd.onlineexam.enumeration.AppExceptionCodeEnum;
import com.ajmd.onlineexam.enumeration.StaticFinalValue;
import com.ajmd.onlineexam.service.DepartmentService;
import com.ajmd.onlineexam.service.TeamService;
import com.ajmd.onlineexam.vo.OrganizationVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ajmd
 * @since 2023/5/2 0:09
 */
@RestController
@RequestMapping("/team")
public class TeamController {
    @Resource
    private TeamService teamService;
    @Resource
    private DepartmentService departmentService;

    @PostMapping("/addOrUpdate")
    public CommonResult addOrUpdate(@RequestBody TeamPO teamPO){
        if(ObjectUtils.isEmpty(teamPO)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        int num = teamService.addOrUpdate(teamPO);
        Boolean isEdit = ObjectUtils.isNotEmpty(teamPO.getId());
        if(num==0){
            if(isEdit){
                return CommonResult.error().message("编辑小组失败！");
            }
            return CommonResult.error().message("新建小组失败！");
        }
        if(isEdit){
            return CommonResult.ok().message("编辑小组成功！");
        }
        return CommonResult.ok().message("新建小组成功！");
    }

    @PostMapping("/select")
    public CommonResult getTeams(@MultiRequestBody Integer userType, @MultiRequestBody Integer currentPage,
                                     @MultiRequestBody Integer pageSize, @MultiRequestBody SearchParam searchParam){

        if(ObjectUtils.isEmpty(userType)){
            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
        }
        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
        }
        Page<TeamPO> pageData = teamService.getPage( currentPage, pageSize, searchParam);
        return CommonResult.ok().data("pageData",pageData);
    }
    @PostMapping("/getList")
    public CommonResult getList(){
        List<TeamPO> listData = teamService.getList(null);
        return CommonResult.ok().data("listData",listData);
    }

//    @PostMapping("selectList")
//    public CommonResult getList(@MultiRequestBody Integer userType){
//        if(ObjectUtils.isEmpty(userType)){
//            return CommonResult.error(AppExceptionCodeEnum.PARAM_ERROR);
//        }
//        if(StaticFinalValue.STUDENT.equals(userType) || StaticFinalValue.TEACHER.equals(userType)){
//            return CommonResult.error(AppExceptionCodeEnum.NOT_AUTHORITY);
//        }
//        List<OrganizationVO> list = departmentService.getList(userType);
//        return CommonResult.ok().data("optionList",list);
//    }

    @PostMapping("/selectOne")
    public CommonResult getTeam(@RequestParam String id){
        TeamPO teamPO = teamService.getOne(id);
        return CommonResult.ok().data("teamPO",teamPO);
    }

    @DeleteMapping("/delete")
    public CommonResult deleteById(@RequestParam String id){
        int num = teamService.delete(id);
        if(num==0){
            return CommonResult.error(AppExceptionCodeEnum.DELETE_ERR);
        }
        return CommonResult.ok().message("小组删除成功");
    }
}
